package com.vjwealth.android.nos.activity.rescue_list

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.vjwealth.android.nos.Constants
import com.vjwealth.android.nos.R
import com.vjwealth.android.nos.activity.rescue_add.RescueAddActivity
import com.vjwealth.android.nos.adapter.RecyclerViewXRefreshAdapter
import com.vjwealth.android.nos.base.BaseApplication
import com.vjwealth.android.nos.base.BaseXRefreshRecyclerActivity
import com.vjwealth.android.nos.entity.RescueListData
import com.vjwealth.android.nos.entity.RescueListItemData
import com.vjwealth.android.nos.http.Api
import kotlinx.android.synthetic.main.activity_refresh_recycler.*
import kotlinx.android.synthetic.main.activity_rescue_list.*
import kotlinx.android.synthetic.main.layout_refresh_recycler.*
import kotlinx.android.synthetic.main.layout_rescue_list_item.view.*
import kotlinx.android.synthetic.main.layout_title.*

class RescueListActivity : BaseXRefreshRecyclerActivity(), RescueListContract.View, View.OnClickListener  {
    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.mRescueAddBtn -> toRescueAddActivity()

        }
    }

    private var mRescueListPresenter: RescueListPresenter? = null

    private var mDataList: MutableList<RescueListItemData>? = null
    private var mAdapter: RecyclerViewXRefreshAdapter<RescueListItemData>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rescue_list)
        mEmptyListView.visibility = View.GONE
        mTitleNameView.text = "救援服务"
        mRescueListPresenter = RescueListPresenter(this)
        mRescueListPresenter!!.fetchRescueList(BaseApplication.spInfor!!.getString(Constants.LOGIN_USER_ID))
    }

    override fun showRescueList(rescueListData: RescueListData) {
        mAdapter!!.setData(rescueListData.rescueList)
        if(rescueListData.showButton) mRescueAddBtn.visibility = View.VISIBLE
        else mRescueAddBtn.visibility = View.GONE
    }

    override fun toRescueAddActivity() {
        var intent = Intent(this@RescueListActivity, RescueAddActivity::class.java)
        startActivity(intent)
    }

    override fun onRecyclerRefresh(isPullDown: Boolean) {}

    override fun onRecyclerLoadMore(isSilence: Boolean) {}

    override fun initRecyclerView() {
        mXRefreshView.pullRefreshEnable = false
        mXRefreshView.pullLoadEnable = false
        mDataList = ArrayList()
        mAdapter = object : RecyclerViewXRefreshAdapter<RescueListItemData>(this@RescueListActivity,
                R.layout.layout_rescue_list_item, mDataList!!){
            override fun convert(holder: CommonViewHolder, bean: RescueListItemData, position: Int) {
                holder.itemView.mRescueItemCarNameView.text = bean.carNumber
                holder.itemView.mRescueItemStatusView.text = bean.status
                holder.itemView.mRescueItemTypeView.text = bean.accidentType
                holder.itemView.mRescueItemAddressView.text = bean.address
                if(bean.cusRescueImages!!.size!=0){
                    holder.itemView.mRescueItemPicLabelView.visibility = View.VISIBLE
                    holder.itemView.mRescueItemPicImage.visibility = View.VISIBLE
                    Glide.with(this@RescueListActivity).load(Api.BASE_IMAGE_URL + bean.cusRescueImages.get(0)).into(holder.itemView.mRescueItemPicImage)
                }else{
                    holder.itemView.mRescueItemPicLabelView.visibility = View.GONE
                    holder.itemView.mRescueItemPicImage.visibility = View.GONE
                }
            }
        }
        mAdapter!!.customLoadMoreView = mXRefreshFooter
        mRecyclerView.layoutManager = mLayoutManager
        mRecyclerView.adapter = mAdapter
    }
}
